<?php 

class Ext_Article_Show
{
    protected $_articleId;
    protected $_excerpt = 0;
    
    public $articleId;
    public $status;
    public $family;
    public $replies;
    public $access;
    public $title;
    public $author;
    public $createDate;
    public $url;
    public $tags;
    public $description;
    public $attachments;
    public $isAll = false;

    public function __construct($articleId)
    {
        $this->_articleId = $articleId;
        
        $this->_build($articleId);
    }
    
    private function _build($articleId)
    {
        $articleRow = App_Model_DbTable_Article::getInstance()->getRowById($articleId);
        if(!$articleRow){
            return "";
        }
        
        $this->articleId    = $articleRow['id'];
        $this->status       = $articleRow['status'];
        $this->replies      = $articleRow['replies'];
        $this->title        = $articleRow['title'];
        $this->author       = Ext_User::getInstance()->show($articleRow['userId'])->buildAvatar()->buildUrl();
        $this->createDate   = new Zend_Date($articleRow['editTime'], Zend_Date::TIMESTAMP);
        $this->url          = Ext_Url::getInstance()->article($articleId);
        $this->access       = new Ext_Article_Show_Access($articleId);
    }
    
    public function isExcerpt()
    {
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article){
            return $this;
        }
        if($article['firstReplyArticleId']){
            return $this;
        }
        
        $this->_excerpt = 1;
        
        return $this;
    }

    public function buildDescription()
    {
        $_articleDescer = App_Model_DbTable_Article_Desc::getInstance();
        $_articleDescRow = $_articleDescer->getRowByArticleId($this->_articleId);
        if(!$_articleDescRow){
            return $this;
        }
        
        $_description = $_articleDescRow['content'];
        if($this->_excerpt){
            $_description = Ext_Utility_String::articleSummary($_description, 212);
        }else{
            $this->isAll = true;
        }
        
        $this->description = $_description;
        
        return $this;
    }
    
    public function buildAttachemnts()
    {
        $_articleRow = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$_articleRow)
        {
            return $this;
        }
        
        $_attachments = array();
        $_orders = unserialize($_articleRow['attachments']);

        for($i=0;$i<count($_orders);$i++)
        {
            if($i>0){
                if($this->_excerpt){
                    break;
                }else{
                    $this->isAll = true;
                }
            }
            $_attachmentType   = $_orders[$i]['type'];
            $_attachmentDataId = $_orders[$i]['id'];
            if(!$_attachmentType || !$_attachmentDataId){
                continue;
            }
            
            $_attachment = new Ext_Article_Show_Attachment($_attachmentType, $_attachmentDataId);
            
            $_attachments[] = $_attachment;
        }
        
        $this->attachments = $_attachments;
        
        return $this;
    }
    
    public function buildTags()
    {
        $_articleTagRow = App_Model_DbTable_Article_Tags::getInstance()->getRowById($this->_articleId);
        if(!$_articleTagRow){
            return $this;
        }
        
        
        $tags = $_articleTagRow['words'];
        $tagArray = explode(";", $tags);
        $tags = array();
        for($i=0;$i<count($tagArray);$i++)
        {
            if(!$tagName = $tagArray[$i])
            {
                continue;
            }
            $tagRow = App_Model_DbTable_Tags::getInstance()->getRowByName($tagName);
            if($tagRow)
            {
                $tags[] = new Ext_Tag_Show($tagRow['id']);
            }
        }
        
        $this->tags = $tags;
        
        return $this;
    }
    
    public function buildFamily()
    {
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article)
        {
            return $this;
        }
        
        if(!$article['firstReplyArticleId'])
        {
            return $this;
        }
        
        $this->family = new Ext_Article_Show_Family($this->_articleId);
        
        return $this;
    }
    
    public function hasFamily()
    {
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article)
        {
            return false;
        }
        
        if(!$article['firstReplyArticleId'])
        {
            return false;
        }
        
        return true;
    }
}